package com.hunau.web.controller.terminal;

import java.util.ArrayList;
import java.util.List;

import com.hunau.common.core.domain.Ztree;
import com.hunau.framework.util.ShiroUtils;
import com.hunau.system.domain.AddrTree;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.hunau.common.annotation.Log;
import com.hunau.common.enums.BusinessType;
import com.hunau.system.domain.Terminals;
import com.hunau.system.service.ITerminalsService;
import com.hunau.common.core.controller.BaseController;
import com.hunau.common.core.domain.AjaxResult;
import com.hunau.common.utils.poi.ExcelUtil;
import com.hunau.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 终端管理（终端管理的主）Controller
 * 
 * @author ruoyi
 * @date 2020-03-15
 */
@Controller
@RequestMapping("/system/terminals")
public class TerminalsController extends BaseController
{
    private String prefix = "system/terminals";

    @Autowired
    private ITerminalsService terminalsService;

    @RequiresPermissions("system:terminals:view")
    @GetMapping()
    public String terminals()
    {
        return prefix + "/terminals";
    }

    @Log(title = "终端管理", businessType = BusinessType.IMPORT)
    @RequiresPermissions("system:terminals:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<Terminals> util = new ExcelUtil<Terminals>(Terminals.class);
        List<Terminals> userList = util.importExcel(file.getInputStream());
        String operName = ShiroUtils.getSysUser().getLoginName();
        String message = terminalsService.importUser(userList, updateSupport, operName);
        return AjaxResult.success(message);
    }
    @RequiresPermissions("system:terminals:view")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<Terminals> util = new ExcelUtil<Terminals>(Terminals.class);
        return util.importTemplateExcel("终端管理");
    }
    /**
     * 查询终端管理（终端管理的主）列表
     */
    @RequiresPermissions("system:terminals:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Terminals terminals)
    {
        startPage();
        //terminals.setIsuse((byte) 1);
        List<Terminals> list = terminalsService.selectTerminalsList(terminals);
        return getDataTable(list);
    }

    /**
     * 导出终端管理（终端管理的主）列表
     */
    @RequiresPermissions("system:terminals:export")
    @Log(title = "终端管理（终端管理的主）", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Terminals terminals, @RequestParam(value = "ids",required = false) ArrayList<Object> ids )
    {
        List<Terminals> list = terminalsService.selectTerminalsListByIds(ids);
        ExcelUtil<Terminals> util = new ExcelUtil<Terminals>(Terminals.class);
        return util.exportExcel(list, "terminals");
    }

    /**
     * 新增终端管理（终端管理的主）
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存终端管理（终端管理的主）
     */
    @RequiresPermissions("system:terminals:add")
    @Log(title = "终端管理（终端管理的主）", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Terminals terminals)
    {
        return toAjax(terminalsService.insertTerminals(terminals));
    }

    /**
     * 修改终端管理（终端管理的主）
     */
    @GetMapping("/edit/{tid}")
    public String edit(@PathVariable("tid") String tid, ModelMap mmap)
    {
        Terminals terminals = terminalsService.selectTerminalsById(tid);
        mmap.put("terminals", terminals);
        return prefix + "/edit";
    }

    /**
     * 修改保存终端管理（终端管理的主）
     */
    @RequiresPermissions("system:terminals:edit")
    @Log(title = "终端管理（终端管理的主）", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Terminals terminals)
    {
        return toAjax(terminalsService.updateTerminals(terminals));
    }

    /**
     * 删除终端管理（终端管理的主）
     */
    @RequiresPermissions("system:terminals:remove")
    @Log(title = "终端管理（终端管理的主）", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(terminalsService.deleteTerminalsByIds(ids));
    }

    /**
     * 加载部门列表树
     */
    @RequiresPermissions("system:terminals:list")
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = terminalsService.selectAddrTree(new AddrTree());
        return ztrees;
    }
}
